package com.heima.admin.service.Imp;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dto.ChannelDTO;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.admin.service.ChannelService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ChannelServiceImp extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    @Override
    public PageResponseResult findByNameAndPage(ChannelDTO dto) {
        //1.参数校验
        dto.checkParam();

        //2分页查询
        Page<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        //查询条件
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        //校验不一定传入的参数 name status
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(AdChannel::getName,dto.getName());
        }
        if (dto.getStatus() != null) {
            wrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //排序
        wrapper.orderByDesc(AdChannel::getOrd);
        //查找
        IPage<AdChannel> channelIPage = this.page(page, wrapper);
        //获得结果
        List<AdChannel> records = channelIPage.getRecords();


        return new PageResponseResult(dto.getPage(),dto.getSize(),page.getTotal(),records);
    }

    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        //1.校验数据 查询频道id
        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道参数不能为空");
        }
        //2.判断频道名称是否存在 存在才能修改
        AdChannel channelOld = getById(adChannel.getId());
        if (channelOld == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"要修改的频道不存在");
        }
        String name = adChannel.getName();
        //判断频道的名称不为空 并且频道跟之前的数据不同在修改
        if (!name.equals(channelOld.getName()) && StringUtils.isNotBlank(name)) {
            //判断频道名称是否超过十个字符
            if (name.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道的长度不能大于10");
            }
            //看修改的名称是否和数据库重复
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
            //判断
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名已存在!");
            }
        }
        //2.更新
        updateById(adChannel);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //数据校验 判空
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE ,"新增频道数据不能为空");
        }
        //判断名字是否为空
        String name = adChannel.getName();
        //判断频道的名称不为空 并且频道跟之前的数据不同在修改
        if (StringUtils.isNotBlank(name)) {
            //判断频道名称是否超过十个字符
            if (name.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道名称的长度不能大于10");
            }
            //看修改的名称是否和数据库重复
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
            //判断
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名已存在!");
            }
        }
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return ResponseResult.okResult();
    }
    @Transactional
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //判断索引是否为空
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"索引不能为空");
        }
        //判断该id对应的索引状态
        AdChannel channel = getById(id);
        //判断当前频道的状态是否为有效
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"当前状态有效不能删除");
        }
        removeById(id);
//        System.out.println(1/0);
    /*    if (id == 47) {
           // throw new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW,"该数据不允许被删除");
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"该数据不允许被删除");
        }*/
        return ResponseResult.okResult();
    }


}
